<template>
  <div>
    <van-nav-bar title="注册页" left-text="返回" left-arrow @click-left="onClickLeft" />
    <!-- 导航功能完成 -->
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field v-model="tel" name="手机号" label="手机号" placeholder="手机号" :rules="[
          { required: true },
          { pattern: /^1[3456789]\d{9}$/, message: '手机号码格式错误！' },
        ]" autocomplete="off" />
        <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]" autocomplete="off" />
        <!-- 验证码及密码输入框 -->
        <van-field v-model="sms" center clearable label="短信验证码" placeholder="请输入短信验证码">
          <template #button>
            <van-button size="small" type="primary" @click="send">发送验证码</van-button>
          </template>
        </van-field>
        <!-- 验证码以及密码输入框 -->
      </van-cell-group>
      <div style="margin: 16px">
        <van-button round block type="primary" native-type="submit">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script setup>
import { sendSMS, register } from "@/services/index";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { useStores } from "@/stores/stores"
const mainStores = useStores()
const router = useRouter();
const onClickLeft = function () {
  history.back();
};
// 注册功能实现的代码
const tel = ref("");
const password = ref("");
const sms = ref("");

// 发送验证码
const send = async function () {
  await sendSMS({ tel: tel.value });
  mainStores.sendmess(tel.value)
  sms.value = mainStores.send;
};

// 提交注册信息
const onSubmit = async function () {
  // 直接注册
  await register({
    tel: tel.value,
    password: password.value,
  });
  console.log("注册成功");
  // showToast('注册成功');
  router.push("/login");

};
</script>
